package com.liwenfeng;

import com.liwenfeng.domain.UserN;
import com.liwenfeng.ui.MonteCarlo;

import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;


public class Pi {
    public static <R> R monteCarlo(Integer num, BiPredicate<Double, Double> predicate, BiFunction<Integer, Integer, R> function) {
        int count = 0;
        for (int i = 0; i < num; i++) {
            double x = Math.random();
            double y = Math.random();
            if (predicate.test(x, y)) {
                count += 1;
            }
        }
        return function.apply(count, num);
    }

    public static void main(String[] args) {
        System.out.println(monteCarlo(10000000, (x, y) -> x * x + y * y <= 1, (c, num) -> Double.valueOf(c) / num * 4).toString());

//        BeanFactory<Object> factory = new BeanFactory<>();
//
//        factory.getBean(()->new Account(3,"zhangsan",99.0));
//        factory.getBean(ArrayList::new);
//        System.out.println(factory.getBean(Account::new));
//        System.out.println(factory.getBean(() -> new Account(4, "lisi", 88.0)));

//        int num = 10000000;
//        int inner_point = 0;
//        for (int i = 0; i < num; i++) {
//            double x = Math.random();
//            double y = Math.random();
//            if ( x * x + y * y <= 1) {
//                inner_point += 1;
//            }
//        }
////        r * r * pi = r * r * (inner_point / num) * 4
//        System.out.println(((double) inner_point / num) * 4.0);
//        Double pi = new Pi().monteCarlo(1000000, (x, y) -> y <= x * x, (c, num) -> c.doubleValue() / num);
//        Double pi = new Pi().monteCarlo(10000000, (x, y) -> x * x + y * y <= 1, (c, num) -> c.doubleValue() / num * 4);
//        Double pi = new Pi().monteCarlo(1000099, (x, y) -> x * x + y * y <= 1, (c,num) -> Double.valueOf(c) / num * 4);
//        System.out.println(pi);

//        Set<UserN> set = new TreeSet<>((o1, o2) -> {
//            return o1.getAge() - o2.getAge() != 0 ? o1.getAge() - o2.getAge() : o2.getName().compareTo(o1.getName())!=0?o1.getName().compareTo(o2.getName()):o1.getHeight().compareTo(o2.getHeight());
////            return o1.getAge() - o2.getAge();
//        });
//        set.addAll(List.of(
//                new UserN("zhangsan", 23,176.5),
//                new UserN("zhangsan", 23,175.5),
//                new UserN("lisi", 22 ,170.2),
//                new UserN("wangwu", 23,165.5),
//                new UserN("zhaolui", 26,160.1),
//                new UserN("luier", 24,180.3)
//        ));
//        for (UserN userN : set) {
//            System.out.println(userN);
//        }
//        MyStream.of(List.of(1, 2, 3, 4, 5))
//                .skip(1)
//                .parallel()
//                .map(item -> {
//                    try {
//                        Thread.sleep(1000);
//                    } catch (InterruptedException e) {
//                        throw new RuntimeException(e);
//                    }
//                    System.out.println(Thread.currentThread().getName()+"====");
//                    return item*2;
//                }).forEach((item)->{
//                    try {
//                        Thread.sleep(1000);
//                    } catch (InterruptedException e) {
//                        throw new RuntimeException(e);
//                    }
//                    System.out.println(Thread.currentThread().getName()+"==="+item);
//                });
//        List.of(1,2,3,4,5).stream().skip(-1).forEach(System.out::println);

    }
    //蒙特卡洛算法


}
